package com.stu.demo;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stu.demo.base.TestBase;
import com.stu.demo.bean.VirutalMachineNode;
import org.junit.Test;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.Value;
import org.neo4j.driver.v1.types.Node;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @projectName: learn_used_code
 * @packageName: com.stu.demo
 * @className: com.stu.demo-> TestNe4jDemo
 * @createDate: 2022-02-17 17:15
 * @author: ZhangSheng
 * @version: 1.0
 * @description: 测试Ne4j数据异常;
 */
public class TestNe4jDemo extends TestBase {


    @Test
    public void testQueryNeo4j() throws Exception{
        //查询
        StatementResult result = this.session.run("MATCH (n:VirtualServer) RETURN n LIMIT 25");
        List<VirutalMachineNode> virutalMachineNodeList = new ArrayList<>();
        while (result.hasNext()) {
            Record record = result.next();
            for(Value value : record.values()){
                if (value.type().name().equals("NODE")){
                    Node node = value.asNode();
                    VirutalMachineNode virutalMachine = new VirutalMachineNode();
                    Iterable<String> labels = node.labels();
                    Iterator<String> iterator = labels.iterator();
                    StringBuilder builder = new StringBuilder();
                    while (iterator.hasNext()){
                        String next = iterator.next();
                        builder.append(next).append(",");
                    }
                    String lables = builder.toString();
                    virutalMachine.setLabel(lables.substring(0,lables.length()-1));
                    virutalMachine.setProperties(node.asMap());
                    virutalMachineNodeList.add(virutalMachine);
                }
            }
        }
        System.out.println(virutalMachineNodeList.get(0).getLabel());
        VirutalMachineNode virutalMachineNode = virutalMachineNodeList.get(0);
        System.out.println(virutalMachineNode.getProperties().toString());
        ObjectMapper objectMapper = new ObjectMapper();
        String valueAsString = objectMapper.writeValueAsString(virutalMachineNode.getProperties());
        VirutalMachineNode virutalMachineNode2 = JSON.parseObject(valueAsString, VirutalMachineNode.class);
        System.out.println(virutalMachineNode2.toString());
    }

    @Test
    public void testCreateNode() throws InterruptedException {
        String cql = "create (:School{id:\"2\",name:\"黄冈一中\",address:\"湖北黄冈\"})";
        Transaction transaction = this.session.beginTransaction();
        try {
            transaction.run(cql);
            transaction.commitAsync();
            transaction.success();
        }catch (Exception e){
            transaction.rollbackAsync();
        }finally {
            transaction.close();
        }

    }

    @Test
    public void testUpdateNodeValue(){
        String cql = "Match (n:Schoole) set n.instanceId='4'";
        Transaction transaction = this.session.beginTransaction();
        try{
            transaction.run(cql);
            transaction.commitAsync();
            transaction.success();
        }catch(Exception e){
            transaction.rollbackAsync();
        }finally {
            transaction.close();
        }
        String cql2 = "Match (n:Schoole) return n.name as name,n.address as address,n.instanceId as instanceId,n.createDate as createDate";
        StatementResult result = this.session.run(cql2);
        while (result.hasNext()) {
            Record next = result.next();
            String name = next.get("name").asString();
            String address = next.get("address").asString();
            String instanceId = next.get("instanceId").asString();
            String createDate = next.get("createDate").asString();
            System.out.println(name + ": "+ address+ ": "+instanceId + ": "+ createDate);
        }
    }

    @Test
    public void testCreateRelationShip(){
        String createRelationShip = "match(s:Student{name:\"zhangsan\"}),(n:Schoole{name:\"蕲春一中\"}) create (s)-[r:RES]->(n) return s,n,r";
        this.session.run(createRelationShip);
    }

    @Test
    public void testDeleteRelationShip(){
        String deleteRelationShip = "match (n:Student{name:\"zhangsan\"})-[r:RES]->() delete r";
        this.session.run(deleteRelationShip);
    }

    @Test
    public void testCreateNodeAndBindingRealationShip(){

        String name = "zhangsan"+ ThreadLocalRandom.current().ints(70, 100).distinct().limit(1).findFirst().getAsInt();

        String uuid = UUID.randomUUID().toString().replaceAll("-","");

        int asInt = ThreadLocalRandom.current().ints(1, 3).distinct().limit(1).findFirst().getAsInt();
        String sex = asInt == 1 ? "男":"女";

        String createNodeSql = "create (:Student{name:\"" + name +"\",uuid: \""+ uuid + "\", sex: \"" + sex + "\"})";

        String createRelationShip = "match(s:Student{uuid:\"" + uuid +"\"}),(n:Schoole{name:\"黄冈一中\"}) create (s)-[r:RES]->(n) return s,n,r";

        Transaction transaction = this.session.beginTransaction();

        try{
            transaction.run(createNodeSql);
            transaction.run(createRelationShip);
            transaction.commitAsync();
            transaction.success();
        }catch (Exception e){
            transaction.rollbackAsync();
        }finally {
            transaction.close();
        }

    }

    @Test
    public void testUpdateNodeData(){
        String cql = "";
    }
}


